<template>
  <div class="app-container">
    <div class="filterDiv">
      <label class="label">辅助核算类型</label>
      <el-select v-model="auxiliaryType" placeholder="辅助核算类型" size="small" @change="getList">
        <el-option v-for="item in typeList" :key="item.auxiliaryTypeCode" :label="item.auxiliaryTypeName" :value="item.auxiliaryTypeCode" />
      </el-select>
      <el-button style="float:right;margin-top:5px;" size="small" type="primary" :disabled="tableData.length===0" round @click="exportBook">导出</el-button>
      <el-button v-permiss="['702080']" style="float:right;margin-top:5px;" size="small" type="primary" round @click="handImport">导入</el-button>
      <el-button v-permiss="['702040']" style="float:right;margin-top:5px" size="small" type="primary" round @click="handleAdd">新增</el-button>
    </div>
    <div class="contentDiv">
      <el-table v-loading="loading" :data="tableData">
        <el-empty slot="empty" :image-size="120" description="暂无核算项" />
        <el-table-column label="核算项代码" align="left" prop="code" min-width="120" />
        <el-table-column label="核算项名称" align="left" prop="name" min-width="150" show-overflow-tooltip />
        <el-table-column label="备注" align="center" prop="remarks" min-width="200" show-overflow-tooltip />
        <el-table-column label="是否启用" align="center" prop="isDisable" min-width="200">
          <template slot-scope="{row}">
            <span v-if="row.isDisable === 0">是</span>
            <span v-if="row.isDisable === 1">否</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button v-permiss="['702050']" size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button v-permiss="['702070']" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="核算项代码" prop="code">
          <el-input v-model="form.code" placeholder="核算项代码" />
        </el-form-item>
        <el-form-item label="核算项名称" prop="name">
          <el-input v-model="form.name" placeholder="核算项名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" :label="0">是</el-radio>
          <el-radio v-model="form.isDisable" :label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="导入辅助核算" :visible.sync="dialogVisible" width="450px">
      <el-form ref="importForm" :model="importForm" label-position="left" label-width="0" style="margin-left:10px;">
        <el-form-item label prop="fileName" :rules="fileRule">
          <el-upload dragle action="/" accept=".xls, .xlsx" :auto-upload="false" :show-file-list="false" :file-list="fileList" :on-change="importFile">
            <div class="fileDiv">
              <span v-if="importForm.fileName" style="margin-right:5px">{{ importForm.fileName }}</span>
              <el-button type="text">{{ fileList.length === 0 ? '点击上传' : '重新上传' }}</el-button>
            </div>
          </el-upload>
        </el-form-item>
        <div style="height:32px" class="tx-l">
          <el-button type="text" @click="downloadTemplate">下载模板</el-button>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" size="small" @click="handleImport()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, nextBdCode } from '@/api/auxiliary'
import { getAuxiliary, saveAuxiliary, delAuxiliary, importAuxiliary, exportAuxiliary } from '@/api/auxiliary'
export default {
  name: 'Dic',
  data() {
    return {
      auxiliaryType: this.$route.query.type || '',
      loading: true,
      total: 0,
      typeList: [],
      tableData: [],
      title: '',
      open: false,
      dialogVisible: false,
      form: {},
      rules: {
        code: [
          { message: '请输入核算项代码', trigger: 'blur', required: true }
        ],
        name: [
          { message: '请输入核算项名称', trigger: 'blur', required: true }
        ]
      },
      importForm: {
        fileName: ''
      },
      fileRule: [
        { required: true, message: '请选择要导入的文件！', trigger: 'change' }
      ],
      fileList: []
    }
  },
  created() {
    listType().then((res) => {
      this.typeList = res.data.data || []
    })
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      const obj = { auxiliaryTypeCode: this.auxiliaryType }
      getAuxiliary(obj).then((res) => {
        this.tableData = res.data.data || []
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        code: null,
        name: null,
        remarks: null,
        isDisable: 0
      }
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加核算项'
      const obj = { auxiliaryTypeCode: this.auxiliaryType }
      nextBdCode(obj).then(res => {
        this.form.code = res.data.data
      })
    },
    handleUpdate(row) {
      this.reset()
      this.form = row
      this.open = true
      this.title = '修改核算项'
    },
    downloadTemplate() {
      location.href = 'business/basedata.xlsx'
    },
    handImport() {
      this.dialogVisible = true
    },
    importFile(file, fileList) {
      this.formData = new FormData()
      if (fileList.length > 0) {
        this.fileList = [fileList[fileList.length - 1]]
      }
      this.formData.append('file', file.raw)
      this.formData.append('fileName', 'auxiliary.xlsx')
      this.formData.append('auxiliaryTypeCode', this.auxiliaryType)
      this.importForm.fileName = file.name
      this.$refs.importForm.validateField('fileName')
    },
    handleImport() {
      this.$refs.importForm.validate((valid) => {
        if (valid) {
          const obj = this.formData
          importAuxiliary(obj).then((res) => {
            if (res.data.errorCode === '0') {
              this.$message.success('导入成功')
              this.dialogVisible = false
              this.getList()
            }
          })
        }
      })
    },
    exportBook() {
      const obj = { auxiliaryTypeCode: this.auxiliaryType }
      exportAuxiliary(obj)
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const obj = this.form
          obj.auxiliaryTypeCode = this.auxiliaryType
          saveAuxiliary(obj).then((res) => {
            this.$message.success('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确定删除吗?', '警告', {
        type: 'warning'
      }).then(() => {
        const obj = { auxiliaryTypeCode: this.auxiliaryType, id: row.id }
        return delAuxiliary(obj)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
